<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\Check\CheckCmbBankFlow;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopLocationModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationExamineLogModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\Feishu\MessageSendService;
use App\Service\OpenShop\Feishu\MessageService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class RollbackSite extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:rollback-site {orderNo}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '回退至选址';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $orderNo = $this->argument('orderNo');
        if (!$orderNo) {
            dd('orderNo不能为空');
        }


        $signOrder = OpenShopSignOrderModel::query()->with('baseInfoRel')->where('orderNo', $orderNo)->first();
        if (!$signOrder) {
            dd('找不到订单');
        }

        DB::beginTransaction();

        OpenShopSiteMissionModel::query()
                                ->where('orderNo', $orderNo)
                                ->where('stepStatus', '>', MissionTemplateEnum::STEP_STATUS_4)
                                ->update(['isDeleted' => 1]);

        OpenShopSiteMissionModel::query()
                                ->where('orderNo', $orderNo)
                                ->where('stepStatus', MissionTemplateEnum::STEP_STATUS_4)
                                ->update(['status' => 1]);

        OpenShopMainProcessModel::query()->where('orderNo', $orderNo)->update([
            'nowStepStatus' => MissionTemplateEnum::STEP_STATUS_4,
            'step'          => MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION,
        ]);

        OpenShopLocationModel::query()->where('orderNo', $orderNo)->update([
            'step' => MainProcessEnum::SHOP_LOCATION_ASSIGN_MANAGER,
        ]);


        $siteLocation = OpenShopSiteLocationModel::query()
            ->whereIn('source',
                [
                    OpenShopSiteLocationModel::SOURCE_OPEN_SHOP,
                    OpenShopSiteLocationModel::SOURCE_HOUSING
                ])->where('orderNo', $orderNo)->first();
        if ($siteLocation->bindId == 0) {
            $siteLocation->clueStatus = OpenShopSiteLocationModel::CLUE_STATUS_0;
        } else {
            $siteLocation->clueStatus = OpenShopSiteLocationModel::CLUE_STATUS_10;
        }

        $siteLocation->siteStatus = OpenShopSiteLocationModel::SITE_STATUS_3;
        $siteLocation->save();

        OpenShopSiteLocationExamineLogModel::query()->where('orderNo', $orderNo)->delete();

        DB::commit();
    }
}
